package com.java1412.controller.admin;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.java1412.entity.User;
import com.java1412.service.UserService;
import com.java1412.utils.ConstantUtil;
import com.java1412.utils.JWTUtil;
import com.java1412.utils.ResponseUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台登录的接口
 *
 * @author java1412
 * @since 2023-08-07 14:48:15
 */
@RestController
@RequestMapping(value = "/admin/user")
//@CrossOrigin
public class AdminUserController {

    @Resource
    private UserService userService;

    // 登录
    @PostMapping(value = "/login")
    public Object login(
//            @RequestParam(value = "username", required = true) String username,
//            @RequestParam(value = "password", required = true) String password
            @RequestBody User user
    ) {
        ResponseUtil r = userService.adminFindByUsername(user.getUsername(), user.getPassword());
        // 生成token
        if (r.getCode().intValue() == 200) {
            // 登录成功,生成token
            // 设置负载
            Map<String, String> map = new HashMap();
            User loginUser = (User) r.getData();
            map.put("userId", loginUser.getId().toString());
            map.put("username", loginUser.getUsername());
            String token = JWTUtil.getToken(map);
            Map<String, Object> data = new HashMap();
            data.put("token", token);
            r.setData(data);
        }
        return r;
    }

    // 查询所有用户信息
    @GetMapping(value = "/find")
    public Object find(
            @RequestParam(value = "username", required = false, defaultValue = "") String username,
            // 第几页
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            // 页大小
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        ResponseUtil r = userService.find(username,currentPage, pageSize);
        return r;
    }

    // 模拟一个接口，这个接口必须处于登录状态才可以访问
    @RequestMapping("/test")
    public ResponseUtil test(String token) {
//        ResponseUtil r = null;
//        try {
//            DecodedJWT decodedJWT = JWTUtil.verify(token);
//            r = ResponseUtil.ok("请求成功");
//        } catch (SignatureVerificationException e) {
//            e.printStackTrace();
//            r = ResponseUtil.fail("无效签名");
//        } catch (TokenExpiredException e) {
//            e.printStackTrace();
//            r = ResponseUtil.fail("token过期");
//        } catch (AlgorithmMismatchException e) {
//            e.printStackTrace();
//            r = ResponseUtil.fail("算法不一致");
//        } catch (Exception e) {
//            e.printStackTrace();
//            r = ResponseUtil.fail("token无效");
//        }
//        return r;
        return ResponseUtil.ok("请求成功");
    }
}
